#include "Time2.h"
#include "stm32f10x_rcc.h"

uint8_t flash_flag;
uint8_t add_flag;
uint8_t sec_flag;
/*
500ms = (arr+1)*(psc+1)/72000 
0.5s = (5000-1+1)*(7200-1+1)/72000000
1s = (10000-1+1)*(7200-1+1)/72000000
*/
void TIM_Init(u16 arr,u16 psc)
{
	TIM_TimeBaseInitTypeDef TimInitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//1.使能定时器时钟
	
	TimInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;//指定时钟分区，没用到
	TimInitStruct.TIM_CounterMode = TIM_CounterMode_Up;//向下计数，与向上计数没有区别
	TimInitStruct.TIM_Period = arr;//重装值
	TimInitStruct.TIM_Prescaler = psc;//预分频值，分72MHz
	TIM_TimeBaseInit(TIM2,&TimInitStruct);//2.初始化定时器
	
	NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	
	NVIC_Init(&NVIC_InitStruct);
	
	TIM_ClearFlag(TIM2, TIM_FLAG_Update);//22、23行可以避免25行执行时进入中断服务函数
//	TIM_ClearITPendingBit(TIM, TIM_FLAG_Update);
	
	TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);//3.定时器中断配置	

	TIM_Cmd(TIM2, ENABLE);//4.使能定时器

}

void TIM2_Disable(void)
{
    TIM_Cmd(TIM2, DISABLE);//4.使能定时器
    TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE);//3.定时器中断配置	
}


//写在stm32f10x_it.c文件里
void TIM2_IRQHandler(void)//5.中断服务程序
{
	if(TIM_GetITStatus(TIM2, TIM_IT_Update)==SET)//判断定时器中断发生
	{
        TIM_ClearITPendingBit(TIM2, TIM_IT_Update);//清除定时器中断标志位
        flash_flag ^= 1;
        add_flag = 1;
        sec_flag = 1;
        
	}
	
		
}
